package com.qipay.sp.excutor.ronghui;

import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiStatusEnum;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.JsonUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class RonghuiUnionQuickExcutor extends AbstractExcutor {

    private final static String KEY_NAME="key";
    private final static String CONFIG_KEY_NAME="key";
    private final static String CONFIG_KEY_DES_NAME="key_des";
    private final static String CONFIG_MCHID_NAME="mch_id";
    private final static String CONFIG_IP_NAME="ip";
    private final static String SIGN_NAME="sign";
    private final static String API_PARAM_SERVER_CALLBACK_URL="serverCallbackUrl";
    private final static String API_PARAM_TRX_TYPE="trxType";
    private final static String API_PARAM_MERCHANT_NO="merchantNo";
    private final static String API_PARAM_ORDER_NUM="orderNum";
    private final static String API_PARAM_AMOUNT="amount";
    private final static String API_PARAM_GOODS_NAME="goodsName";
    private final static String API_PARAM_ORDER_IP="orderIp";
    private final static String API_PARAM_ENCRYPT="encrypt";
    private final static String API_PARAM_CARD_NO="cardNo";
    private final static String API_PARAM_MOBILE ="payerPhone";
    private final static String API_PARAM_TYPE="kuaiType";
    private final static String API_URL="http://trx.ronghuijinfubj.com/middlepaytrx/online/kuai/pay";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.UNION_QUICK;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(API_PARAM_ORDER_NUM, order.getTradeNo());
        apiParams.put(API_PARAM_GOODS_NAME, order.getBody());
        apiParams.put(API_PARAM_AMOUNT, new BigDecimal(order.getAmount()).divide(new BigDecimal(100)));

        apiParams.put(API_PARAM_CARD_NO,extendOrderParams.get("card_no"));
        apiParams.put(API_PARAM_MOBILE,extendOrderParams.get("mobile"));
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object key = getVal(spconfig, CONFIG_KEY_NAME);
        Object orderIp = getVal(spconfig, CONFIG_IP_NAME);
        Object trxType = getVal(params, API_PARAM_TRX_TYPE);
        Object orderNum = getVal(params, API_PARAM_ORDER_NUM);
        Object merchantNo = getVal(spconfig, CONFIG_MCHID_NAME);
        Object amount = getVal(params, API_PARAM_AMOUNT);
        Object goodsName = getVal(params, API_PARAM_GOODS_NAME);
        Object serverCallbackUrl = getVal(params, API_PARAM_SERVER_CALLBACK_URL);
        Object cardNo = getVal(params, API_PARAM_CARD_NO);
        Object payerPhone = getVal(params, API_PARAM_MOBILE);
        Object kuaiType = getVal(params, API_PARAM_TYPE);
        Object encrypt = getVal(params, API_PARAM_ENCRYPT);

        String signData = "#" + trxType + "#" + merchantNo + "#" + orderNum + "#" + amount + "#"
                + goodsName + "#" + serverCallbackUrl+ "#" + orderIp + "#" + cardNo + "#" + payerPhone + "#" + kuaiType + "#" + encrypt + "#" + key;

        String sign = DigestUtils.md5Hex(signData);
        logger.info("签名串：{}，签名：{}",signData,sign);

        params.put(API_PARAM_MERCHANT_NO,merchantNo);
        params.put(API_PARAM_ORDER_IP,orderIp);
        params.put(SIGN_NAME, sign);

        String result = sendPostByKeyValue(params, RonghuiUnionQuickExcutor.API_URL);
        if(StringUtils.isBlank(result)){
            throw new Exception("请求ronghui快捷支付接口失败");
        }

        Map<String, Object> responseMap = JsonUtils.toMap(result);
        Map<String, Object> resultMap = new HashMap<>();

        if(responseMap == null || !responseMap.containsKey("retCode") || responseMap.get("retCode") == null )
        {
            logger.error("请求ronghui快捷支付接口失败:" + result);
            return null;
        }

        if(!(responseMap.get("retCode").toString().equals("0000") || responseMap.get("retCode").toString().equals("1000"))) {
            logger.error("请求ronghui快捷支付接口业务执行失败:" + result);
            resultMap.put("result_code", "1");
            resultMap.put("err_code", ApiStatusEnum.SP_ERR.getCode());
            resultMap.put("err_msg", responseMap.get("retMsg"));
            return resultMap;
        }

        resultMap.put("result_code", "0");
        resultMap.put("code_url", responseMap.get("qrCode"));
        return resultMap;
    }
}
